

package module.blog

class LinkController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ linkInstanceList: Link.list( params ), linkInstanceTotal: Link.count() ]
    }

    def show = {
        def linkInstance = Link.get( params.id )

        if(!linkInstance) {
            flash.message = "Link not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ linkInstance : linkInstance ] }
    }

    def delete = {
		Link.withTransaction {
	        def linkInstance = Link.get( params.id )
	        if(linkInstance) {
	            try {
	                linkInstance.delete(flush:true)
	                flash.message = "Link ${params.id} deleted"
	                redirect(action:list)
	            }
	            catch(org.springframework.dao.DataIntegrityViolationException e) {
	                flash.message = "Link ${params.id} could not be deleted"
	                redirect(action:show,id:params.id)
	            }
	        }
	        else {
	            flash.message = "Link not found with id ${params.id}"
	            redirect(action:list)
	        }			
		}
    }

    def edit = {
        def linkInstance = Link.get( params.id )

        if(!linkInstance) {
            flash.message = "Link not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ linkInstance : linkInstance ]
        }
    }

    def update = {
		Link.withTransaction {
	        def linkInstance = Link.get( params.id )
	        if(linkInstance) {
	            if(params.version) {
	                def version = params.version.toLong()
	                if(linkInstance.version > version) {
	                    
	                    linkInstance.errors.rejectValue("version", "link.optimistic.locking.failure", "Another user has updated this Link while you were editing.")
	                    render(view:'edit',model:[linkInstance:linkInstance])
	                    return
	                }
	            }
	            linkInstance.properties = params
	            if(!linkInstance.hasErrors() && linkInstance.save()) {
	                flash.message = "Link ${params.id} updated"
	                redirect(action:show,id:linkInstance.id)
	            }
	            else {
	                render(view:'edit',model:[linkInstance:linkInstance])
	            }
	        }
	        else {
	            flash.message = "Link not found with id ${params.id}"
	            redirect(action:list)
	        }			
		}
    }

    def create = {
        def linkInstance = new Link()
        linkInstance.properties = params
        return ['linkInstance':linkInstance]
    }

    def save = {
        def linkInstance = new Link(params)
		Link.withTransaction {
	        if(linkInstance.save(flush:true)) {
	            flash.message = "Link ${linkInstance.id} created"
	            redirect(action:show,id:linkInstance.id)
	        }
	        else {
	            render(view:'create',model:[linkInstance:linkInstance])
	        }
		}
    }
}
